﻿namespace Cos.Core.NHibernate.Util
{
    using System.Collections.Generic;
    using System.Linq;

    using Cos.Core.Model.Common;
    using Cos.Core.Model.Form;

    public static class FormModelConverter
    {
        public static Form ToForm(this FormDataModel formDataModel, string cultureCode, bool isLiveMode)
        {
            var form = new Form();

            form.Id = formDataModel.Id;
            form.AnswerOnce = formDataModel.AnswerOnce;
            form.ApprobationDate = formDataModel.ApprobationDate;
            form.Author = formDataModel.Author;
            form.ConfirmEmail = formDataModel.ConfirmEmail;
            form.CreationDate = formDataModel.CreationDate;
            form.CSS = formDataModel.CSS;
            form.DisplayType = formDataModel.DisplayType;
            form.EmailFrom = formDataModel.EmailFrom;
            form.IsDeleted = formDataModel.IsDeleted;
            form.Kickback = formDataModel.Kickback;
            form.LastModifiedDate = formDataModel.LastModifiedDate;
            form.Name = formDataModel.Name;
            form.PageCSS = formDataModel.PageCss;
            form.ReturnURL = formDataModel.ReturnUrl;
            form.SelectedPageCSS = formDataModel.SelectedPageCss;
            form.ShowAsSurvey = formDataModel.ShowAsSurvey;
            form.ShowSummary = formDataModel.ShowSummary;
            form.CultureCode = cultureCode;

            return form;
        }

        public static FormDataModel ToFormDataModel(this Form form, bool isLiveMode)
        {
            var formDataModel = new FormDataModel();
            formDataModel.Id = form.Id;
            formDataModel.AnswerOnce = form.AnswerOnce;
            formDataModel.ApprobationDate = form.ApprobationDate;
            formDataModel.Author = form.Author;
            formDataModel.ConfirmEmail = form.ConfirmEmail;
            formDataModel.CreationDate = form.CreationDate;
            formDataModel.CSS = form.CSS;
            formDataModel.DisplayType = form.DisplayType;
            formDataModel.EmailFrom = form.EmailFrom;
            formDataModel.IsDeleted = form.IsDeleted;
            formDataModel.Kickback = form.Kickback;
            formDataModel.LastModifiedDate = form.LastModifiedDate;
            formDataModel.Name = form.Name;
            formDataModel.PageCss = form.PageCSS;
            formDataModel.ReturnUrl = form.ReturnURL;
            formDataModel.SelectedPageCss = form.SelectedPageCSS;
            formDataModel.ShowAsSurvey = form.ShowAsSurvey;
            formDataModel.ShowSummary = form.ShowSummary;

            var formCultureDataModel = new FormCultureDataModel();
            formCultureDataModel.Status = form.Status;

            if (isLiveMode)
            {
                formCultureDataModel.CompletedTextLive = form.CompletedText;
                formCultureDataModel.EmailSubjectLive = form.EmailSubject;
                formCultureDataModel.IsVisibleLive = form.IsVisible;
            }
            else
            {
                formCultureDataModel.CompletedText = form.CompletedText;
                formCultureDataModel.EmailSubject = form.EmailSubject;
                formCultureDataModel.IsVisiblePreview = form.IsVisible;
            }


            return formDataModel;
        }

        public static Page ToPage(this FormPageDataModel formPageDataModel, string cultureCode, bool isLiveMode)
        {
            var page = new Page();

            page.Id = formPageDataModel.Id;
            page.FormId = formPageDataModel.FormId;
            page.IsDeleted = formPageDataModel.IsDeleted;
            page.Ordering = formPageDataModel.Ordering;
            page.CultureCode = cultureCode;

            return page;
        }

        public static FormPageDataModel ToFormPageDataModel(this Page page, bool isLiveMode)
        {
            var formPageDataModel = new FormPageDataModel();
            formPageDataModel.Id = page.Id;
            formPageDataModel.FormId = page.FormId;
            formPageDataModel.IsDeleted = page.IsDeleted;
            formPageDataModel.Ordering = page.Ordering;

            var formPageCultureDataModel = new FormPageCultureDataModel();
            formPageCultureDataModel.Status = page.Status;
            if (isLiveMode)
            {
                formPageCultureDataModel.NameLive = page.Name;
                formPageCultureDataModel.IsVisibleLive = page.IsVisible;
            }
            else
            {
                formPageCultureDataModel.NamePreview = page.Name;
                formPageCultureDataModel.IsVisiblePreview = page.IsVisible;
            }

            return formPageDataModel;
        }

        public static Element ToElement(this FormElementDataModel formElementDataModel, string cultureCode, bool isLiveMode)
        {
            var element = new Element();

            element.Id = formElementDataModel.Id;
            element.FormId = formElementDataModel.FormId;
            element.PageId = formElementDataModel.FormId;
            element.Boolean1 = isLiveMode ? formElementDataModel.Boolean1Live : formElementDataModel.Boolean1;
            element.Boolean2 = isLiveMode ? formElementDataModel.Boolean2Live : formElementDataModel.Boolean2;
            element.CSS = isLiveMode ? formElementDataModel.CssLive : formElementDataModel.CSS;
            element.Enum = isLiveMode ? formElementDataModel.EnumLive : formElementDataModel.Enum;
            element.InputType = formElementDataModel.InputType;
            element.IsDeleted = formElementDataModel.IsDeleted;
            element.Long1 = isLiveMode ? formElementDataModel.Long1Live : formElementDataModel.Long1;
            element.Long2 = isLiveMode ? formElementDataModel.Long2Live : formElementDataModel.Long2;
            element.Long3 = isLiveMode ? formElementDataModel.Long3Live : formElementDataModel.Long3;
            element.Long4 = isLiveMode ? formElementDataModel.Long4Live : formElementDataModel.Long4;
            element.Name = formElementDataModel.Name;
            element.Ordering = formElementDataModel.Ordering;
            element.Required = isLiveMode ? formElementDataModel.RequiredLive : formElementDataModel.Required;
            element.Text1 = isLiveMode ? formElementDataModel.Text1Live : formElementDataModel.Text1;
            element.Text2 = isLiveMode ? formElementDataModel.Text2Live : formElementDataModel.Text2;
            element.CultureCode = cultureCode;

            return element;
        }

        public static FormElementDataModel ToElementDataModel(this Element element,bool isLiveMode)
        {
            var formElementDataModel = new FormElementDataModel();
            formElementDataModel.Id = element.Id;
            formElementDataModel.FormId = element.FormId;
            formElementDataModel.FormId = element.PageId;

            if (isLiveMode)
            {
                formElementDataModel.Boolean1Live = element.Boolean1;
                formElementDataModel.Boolean2Live = element.Boolean2;
                formElementDataModel.CssLive = element.CSS;
                formElementDataModel.EnumLive = element.Enum;
                formElementDataModel.Long1Live = element.Long1;
                formElementDataModel.Long2Live = element.Long2;
                formElementDataModel.Long3Live = element.Long3;
                formElementDataModel.Long4Live = element.Long4;
                formElementDataModel.Text1Live = element.Text1;
                formElementDataModel.Text2Live = element.Text2;

            }
            else
            {
                formElementDataModel.Boolean1 = element.Boolean1;
                formElementDataModel.Boolean2 = element.Boolean2;
                formElementDataModel.CSS = element.CSS;
                formElementDataModel.Enum = element.Enum;
                formElementDataModel.Long1 = element.Long1;
                formElementDataModel.Long2 = element.Long2;
                formElementDataModel.Long3 = element.Long3;
                formElementDataModel.Long4 = element.Long4;
                formElementDataModel.Text1 = element.Text1;
                formElementDataModel.Text2 = element.Text2;
            }
            formElementDataModel.Name = element.Name;
            formElementDataModel.Ordering = element.Ordering;



            return formElementDataModel;
        }

        public static Choice ToChoice(this FormElementChoiceDataModel formElementChoiceDataModel, string cultureCode, bool isLiveMode)
        {
            var choice = new Choice();

            choice.Id = formElementChoiceDataModel.Id;
            choice.ElementId = formElementChoiceDataModel.FormElementId;
            choice.IsDeleted = formElementChoiceDataModel.IsDeleted;
            choice.Ordering = formElementChoiceDataModel.Ordering;
            choice.Value = formElementChoiceDataModel.Value;
            choice.CultureCode = cultureCode;

            return choice;
        }

        public static FormElementChoiceDataModel ToChoiceDataModel(this Choice choice, bool isLiveMode)
        {
            var formElementChoiceDataModel = new FormElementChoiceDataModel();

            formElementChoiceDataModel.Id = choice.Id;
            formElementChoiceDataModel.FormElementId = choice.ElementId;
            formElementChoiceDataModel.IsDeleted = choice.IsDeleted;
            formElementChoiceDataModel.Ordering = choice.Ordering;
            formElementChoiceDataModel.Value = choice.Value;

            return formElementChoiceDataModel;
        }
    }
}
